﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BT_DoHoa_Nhom13.ShapeLib;

namespace BT_DoHoa_Nhom13.EffectLib
{
    public class BorderEffect : MyEffect
    {
        const int RADIUS = 5;
        static MyEffect instance;
        List<MyShape> lstMiniShapes;
        public static MyEffect GetInstance()
        {
            if (instance == null)
            {
                instance = new BorderEffect();
            }

            return instance;
        }

        private BorderEffect()
        {
            name = "Border";
            kind = KIND.TOP;
            lstMiniShapes = new List<MyShape>();

            lstMiniShapes.Add(new MyEllipse());
            lstMiniShapes.Add(new MyRectangle());
            lstMiniShapes.Add(new MyEllipse());
            lstMiniShapes.Add(new MyRectangle());

            lstMiniShapes.Add(new MyRectangle());
            lstMiniShapes.Add(new MyEllipse());
            lstMiniShapes.Add(new MyRectangle());
            lstMiniShapes.Add(new MyEllipse());
        }

        public override void Draw(MyGraphic graphic, MyShape shape)
        {
            CreateBorder(shape);
            foreach(MyShape miniShape in lstMiniShapes)
            {
                miniShape.Draw(graphic,null);
            }
        }

        private void CreateBorder(MyShape shape)
        {
            int top, left, width, height;
            shape.SetConfig(out top, out left, out width, out height);
            int count = 0;
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    if ((i > 0 && i < 2) && (j > 0 && j < 2))
                    {
                        continue;
                    }

                    MyShape miniShape = lstMiniShapes[count];
                    int centerX, centerY;
                    centerX = left + j * width / 2;
                    centerY = top + i * height / 2;
                    this.SetBorderForEachShape(miniShape, centerX, centerY);
                   
                    count++;
                }
            }
        }

        private void SetBorderForEachShape(MyShape shape, int centerX, int centerY)
        {
            int newStartX = centerX - RADIUS;
            int newEndX = centerX + RADIUS;
            int newStartY = centerY - RADIUS;
            int newEndY = centerY + RADIUS;

            shape.SetStartPoint(newStartX, newStartY);
            shape.SetEndPoint(newEndX, newEndY);
        }
    }
}
